const fs = require('fs')
const path = require('path')
const { Note } = require('../model/note')

function sortFn (a, b) {
  if (a.isDir) return 0
  else return 1
}

// 去掉以.开始的隐藏文件
function getNotes (p) {
  let files = fs.readdirSync(p)
  let notes = []
  for (let file of files) {
    if (file.startsWith('.')) {
      continue
    }
    let stat = fs.statSync(path.join(p, file))

    let note = new Note(p, file, stat.isDirectory(path.join(p, file), []))
    if (note.isDir) {
      note.children = getNotes(path.join(p, note.name))
      note.children.sort(sortFn)
    }
    notes.push(note)
  }
  notes.sort(sortFn)
  return notes
}

module.exports = { getNotes }
